package org.arabidopsis.plantCyc;

import java.io.*;
import java.util.*;

import org.biojava.bio.program.sax.*;
import org.biojava.bio.program.ssbind.*;
import org.biojava.bio.search.*;
import org.biojava.bio.program.sax.blastxml.BlastXMLParserFacade;
import org.biojava.bio.seq.db.*;
import org.xml.sax.*;
import org.biojava.bio.*;

//input /home/ozgur/brenda/tempresult.xml or
//		input.xml
public class BlastAndParse {
  /**
   * args[0] is assumed to be the name of a Blast output file
   */
  public static void main(String[] args) {
    try {
    	Runtime rt = Runtime.getRuntime();
		
			//Process proc = rt.exec ("/home/ozgur/brenda/.blast");
			Process proc = 
				rt.exec ("/home/ozgur/blast-2.2.17/bin/blastall " +
						"-p blastp -d /home/ozgur/brenda/brAll -i /home/ozgur/brenda/tester -b 300 -m 7");
			// if you have set the env var BLASTDB=/home/ozgur/brenda/brAll
			// /home/ozgur/blast-2.2.17/bin/blastall -p blastp -d $BLASTDB .......
			// 
			// another version of exec rt.exec (new String[] {"sh","-c",cmd});				
		
	  //get the Blast input as a Stream
      //InputStream is = new FileInputStream(args[0]);
  	  InputStream is = proc.getInputStream();
      //make a BlastLikeSAXParser
      BlastXMLParserFacade parser = new BlastXMLParserFacade();

      //make the SAX event adapter that will pass events to a Handler.
      SeqSimilarityAdapter adapter = new SeqSimilarityAdapter();

      //set the parsers SAX event adapter
      parser.setContentHandler(adapter);

      //The list to hold the SeqSimilaritySearchResults
      List results = new ArrayList();

      //create the SearchContentHandler that will build SeqSimilaritySearchResults
      //in the results List
      SearchContentHandler builder = new BlastLikeSearchBuilder(results,
          new DummySequenceDB("queries"), new DummySequenceDBInstallation());

      //register builder with adapter
      adapter.setSearchContentHandler(builder);

      //parse the file, after this the result List will be populated with
      //SeqSimilaritySearchResults
      parser.parse(new InputSource(is));

      //output some blast details
      for (Iterator i = results.iterator(); i.hasNext(); ) {
//        System.out.println("loop");
        SeqSimilaritySearchResult result =
            (SeqSimilaritySearchResult)i.next();

        Annotation anno = result.getAnnotation();

        for (Iterator j = anno.keys().iterator(); j.hasNext(); ) {
          Object key = j.next();
          Object property = anno.getProperty(key);
          System.out.println(key+" : "+property);
        }
        System.out.println("Hits: ");

        //list the hits
        for (Iterator k = result.getHits().iterator(); k.hasNext(); ) {
          SeqSimilaritySearchHit hit =
              (SeqSimilaritySearchHit)k.next();
          System.out.print("\tmatch: "+hit.getSubjectID());
          System.out.println("\te score: "+hit.getEValue());
        }

        System.out.println("\n");
      }

      		InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            System.out.println("<ERROR>");
            while ( (line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</ERROR>");
      
      int exitVal = proc.waitFor();
      System.out.println("Process exitValue: " + exitVal);
		System.out.println("done");
    }
    catch (SAXException ex) {
      //XML problem
      ex.printStackTrace();
    }catch (IOException ex) {
      //IO problem, possibly file not found
      ex.printStackTrace();
    }
    catch (BioException be) {
      be.printStackTrace();
    }
	catch (InterruptedException e) {
		e.printStackTrace();}
	catch (Throwable t)
    {
      t.printStackTrace();
    }

  }
}
